import requests
from apiAutomation import app



class LoginApi:

    # 初始化
    def __init__(self):
        self.login_url = app.BATH_URL + "/login"
        self.login_out_url = app.BATH_URL + "/logout/{}"

    # 登录接口
    def login(self, user, password):
        data = {
            "username": user,
            "password": password
        }
        return requests.post(url=self.login_url, headers=app.headers, json=data)

    # 退出登录接口
    def login_out(self, user_id, token):
        app.headers["token"] = token
        # 拼接新的url
        new_login_out_url = self.login_out_url.format(user_id)
        return requests.post(url=new_login_out_url, headers=app.headers)

    # 获取token
    def get_token(self):
        data = {
            "username": "admin",
            "password": "123"
        }
        res_login = requests.post(url=self.login_url, headers=app.headers, json=data)
        token = res_login.json()["resultData"].get("token")
        return token


if __name__ == '__main__':
    print(LoginApi().login("admin", "123").json())
    print(LoginApi().get_token())
    LoginApi()